package com.gosuncn.controller;

import com.gosuncn.config.pdf.AbstractHtmlToPdf;
import com.gosuncn.pojo.ExportDto;
import com.gosuncn.utils.FileUtil;

import com.gosuncn.utils.QRCodeUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.util.Calendar;
import java.util.Date;


/**
 * @Description: pdf
 * @Author: xiaolong
 * @date: 2022/3/25 11:33
 * @Version: 1.0
 */
@RequestMapping("/api/callPolice")
@RestController
public class PDFController {

    private final static Logger log = LoggerFactory.getLogger(PDFController.class);


    @Autowired
    private AbstractHtmlToPdf htmlToPdf;


    /**
     * @Description: 导出报警回执word
     * @param:
     * @return:
    */
    @GetMapping(value = "/exportWord")
    public void exportWord(@NotBlank(message = "报警单号") String id, HttpServletResponse response){
        try {
            ExportDto exportDto = new ExportDto();
            exportDto.setId("12345678");
            exportDto.setName("张先生");
            exportDto.setClrSfzh("1324587845");
            exportDto.setEwm("123456844");
            exportDto.setBoxOrgName("广东省广州市黄埔区");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            exportDto.setYear(Integer.toString(calendar.get(Calendar.YEAR)));
            //%02d指输出2位十进制数，右端对齐，不够位数的话左端补0.
            exportDto.setMonth(String.format("%02d", calendar.get(Calendar.MONTH) + 1));
            exportDto.setDay(String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH)));
            exportDto.setHour(String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)));
            exportDto.setMin(String.format("%02d", calendar.get(Calendar.MINUTE)));
            Configuration configuration = new Configuration(Configuration.getVersion());
            configuration.setClassForTemplateLoading(PDFController.class,"/");
            Template template = configuration.getTemplate("test3.xml", "UTF-8");
            String result = FreeMarkerTemplateUtils.processTemplateIntoString(template, exportDto);
            FileUtil.exportWord(result,String.format("报警回执%s",".doc"),response);
        }catch (Exception e){
            log.error("导出文件失败{}",e.getMessage());
        }
    }



    /**
     * @Description: 预览报警回执PDF
     * @param:
     * @return:
    */
    @GetMapping(value = "/previewPdf")
    public void previewPdf( String id, HttpServletResponse response){
        try {
            ExportDto exportDto = new ExportDto();
            exportDto.setId("12345678");
            exportDto.setName("张先生");
            exportDto.setClrSfzh("1324587845");
            exportDto.setEwm("123456844");
            exportDto.setBoxOrgName("广东省广州市黄埔区");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            exportDto.setYear(Integer.toString(calendar.get(Calendar.YEAR)));
            //%02d指输出2位十进制数，右端对齐，不够位数的话左端补0.
            exportDto.setMonth(String.format("%02d", calendar.get(Calendar.MONTH) + 1));
            exportDto.setDay(String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH)));
            exportDto.setHour(String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)));
            exportDto.setMin(String.format("%02d", calendar.get(Calendar.MINUTE)));

            //二维码信息
            String qrBase64 = QRCodeUtil.getQRBase64("欢迎光临", 400, 400);
            exportDto.setEwm(qrBase64);

            //创建一个FreeMarker实例,负责管理FreeMarker模板
            Configuration configuration = new Configuration(Configuration.getVersion());
            //设置模板文件位置默认路径为resources下
            configuration.setClassForTemplateLoading(ExportDto.class,"/");
            if (new File(System .getProperty("user.dir") + "\\config\\temp.xml").exists()){
                configuration.setDirectoryForTemplateLoading(new File(System .getProperty("user.dir") + "\\config"));//决对路径
            }else if (new File(System .getProperty("user.dir") + "\\temp.xml").exists()){
                configuration.setDirectoryForTemplateLoading(new File(System .getProperty("user.dir")));//决对路径
            }
            //获取模板文件
            Template template = configuration.getTemplate("test3.xml", "UTF-8");
            //渲染模板
            String result = FreeMarkerTemplateUtils.processTemplateIntoString(template, exportDto);
            FileUtil.previewPDF(result,String.format("报警回执%s",".pdf"),response);
        }catch (Exception e){
            log.error("导出文件失败{}",e.getMessage());
            response.setStatus(HttpStatus.SC_NOT_FOUND);
        }
    }



}
